package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @author zhaoxw
 * @description TODO
 * @date 2024/1/29 14:22
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * @return
     * @Author zhaoxw
     * @Description //切入点
     * @Date 14:25 2024/1/29
     **/

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * @return
     * @Author zhaoxw
     * @Description //前置通知
     * @Date 14:29 2024/1/29
     **/
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        //joinPoint是切点

        log.info("开始公共字段自行填充");

        //    @AutoFill(value = OperationType.UPDATE)
        //    void update(Category category);
        //第一步：利用反射找到数据库的操作类型   如@AutoFill(value = OperationType.UPDATE)中是update操作
        //方法获得签名对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获得注解对象    这里是@AutoFill
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        //获得注解对象的值
        OperationType operationType = autoFill.value();
        //获得被拦截方法参数 实体对象，判断实体对象是否为空，空的话返回，不为空进行第二步
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        //准备赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //第二步：找到拦截的方法的参数--实体对象Category category
        Object entity = args[0];

        //第三步：根据当前不同的操作类型，为对应的属性通过反射来赋值
        //如果是插入操作
        if (operationType == OperationType.INSERT) {
            //entity.getClass().getDeclaredMethod(方法名,参数)；
            try {
                Method setCreateTimeMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, now.getClass());
                //Method setCreateUserMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, currentId.getClass());
                Method setUpdateTimeMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, now.getClass());
               // Method setUpdateUserMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, currentId.getClass());

                //通过反射对实体对象 赋值
                setCreateTimeMethod.invoke(entity, now);
                //setCreateUserMethod.invoke(entity, currentId);
                setUpdateTimeMethod.invoke(entity, now);
               // setUpdateUserMethod.invoke(entity, currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (operationType == OperationType.UPDATE) {
            try {
                Method setUpdateTimeMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, now.getClass());
               // Method setUpdateUserMethod = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, currentId.getClass());
                setUpdateTimeMethod.invoke(entity, now);
               // setUpdateUserMethod.invoke(entity, currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

}
